/*---------------------------------------------------------------------------*
 * Copyright (C) 2011-2014 Ryan M. Murphy <ryan.m.murphy.77@gmail.com>
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * File Name   : usr_consoleCharger.c
 *
 * Project     : Watts-rock
 *               <http://code.google.com/p/watts-rock-motherboard/>
 *
 * Description : This file is responsible for command and control over the
 *               console module from a user-space perspective.
 *
 * Programmer  : Ryan M Murphy
 *
 * Last Update : Jan 4, 2014
 *---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*
 * Include Files
 *---------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "arb_error.h"
#include "arb_thread.h"
#include "arb_semaphore.h"
#include "arb_device.h"
#include "arb_sysTimer.h"
#include "arb_console.h"
#include "arb_printf.h"
#include "drv_console.h"
#include "usr_chrg.h"

/*---------------------------------------------------------------------------*
 * Private Global Variables
 *---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------*
 * Private Function Definitions
 *---------------------------------------------------------------------------*/
static void usr_displayUserHelp( t_DEVHANDLE t_consoleHndl,
                                 int8_t *pc_buff)
{
   uint16_t s_size;

   /*------------------------------------------------------------------------*
    * Display a list of all the possible console commands specific
    * to a user-space application.
    *------------------------------------------------------------------------*/
   s_size = sprintf_P( (char *)pc_buff, PSTR(".-------------------------------------------------------------------------.\n\r"));
   arb_write( t_consoleHndl,
              pc_buff,
              s_size);
   s_size = sprintf_P( (char *)pc_buff, PSTR("| Command |     Arguments      |              Description                 |\n\r"));
   arb_write( t_consoleHndl,
              pc_buff,
              s_size);
   s_size = sprintf_P( (char *)pc_buff, PSTR("|---------|--------------------|------------------------------------------|\n\r"));
   arb_write( t_consoleHndl,
              pc_buff,
              s_size);
   s_size = sprintf_P( (char *)pc_buff, PSTR("| fan     | <arg1>             | Turns on/off the fan, where              |\n\r"));
   arb_write( t_consoleHndl,
              pc_buff,
              s_size);
   s_size = sprintf_P( (char *)pc_buff, PSTR("| mcm     | <arg1>             | Manual charge mode, where 0 <= <arg1> < 1|\n\r"));
   arb_write( t_consoleHndl,
              pc_buff,
              s_size);
   s_size = sprintf_P( (char *)pc_buff, PSTR("'---------'--------------------'------------------------------------------'\n\r"));
   arb_write( t_consoleHndl,
              pc_buff,
              s_size);

}/*End usr_displayUserHelp*/

/*---------------------------------------------------------------------------*
 * Public Function Definitions
 *---------------------------------------------------------------------------*/
bool usr_console( t_DEVHANDLE t_consoleHndl,
                  int8_t *pc_buff,
                  t_consoleTokHndl *pt_tokHndl)
{
   bool b_success = true; /*A valid command was found.*/
   uint16_t s_size;

   if( strcmp_P( (char *)pt_tokHndl->ac_tok[0], PSTR("hlpu")) == 0)
   {
      /*---------------------------------------------------------------------*
       * Display a list of all the possible user-space specific commands.
       *---------------------------------------------------------------------*/
      usr_displayUserHelp( t_consoleHndl,
                           pc_buff);

   }/*End else if( strcmp( (char *)pt_tokHndl->ac_tok[0], "hlpu") == 0)*/
   else if( (strcmp_P( (char *)pt_tokHndl->ac_tok[0], PSTR("fan")) == 0)
   && (pt_tokHndl->c_numTokens == 2))
   {
      int16_t s_numBytes;
      t_MAILBOXHNDL t_chrgMlbxHndl = usr_getBmsInMlbxHndle();
      t_chrgFanCntrlMsg t_fanCntl;

      t_fanCntl.c_msgId = CHRG_CMD_FAN_CNTRL;

      if( pt_tokHndl->ac_tok[1][0] == '0')
      {
         t_fanCntl.b_enabled = false;
         s_numBytes = arb_mailboxWrite( t_chrgMlbxHndl,
         (int8_t *)&t_fanCntl,
         (int16_t)sizeof( t_chrgFanCntrlMsg));

      }
      else if( pt_tokHndl->ac_tok[1][0] == '1')
      {
         t_fanCntl.b_enabled = true;
         s_numBytes = arb_mailboxWrite( t_chrgMlbxHndl,
         (int8_t *)&t_fanCntl,
         (int16_t)sizeof( t_chrgFanCntrlMsg));

      }
      else
      {
         s_size = sprintf_P( (char *)pc_buff, PSTR("Invalid argument\n\r"));
         arb_write( t_consoleHndl,
         pc_buff,
         s_size);
      }

   }
   else if( (strcmp_P( (char *)pt_tokHndl->ac_tok[0], PSTR("mcm")) == 0)
   && (pt_tokHndl->c_numTokens == 2))
   {
      int16_t s_numBytes;
      t_MAILBOXHNDL t_chrgMlbxHndl = usr_getBmsInMlbxHndle();
      t_chrgManChrgMsg t_chrgCntl;

      t_chrgCntl.c_msgId = CHRG_CMD_MAN_CHRG_CNTRL;
      t_chrgCntl.f_pwm = atof( (char *)&pt_tokHndl->ac_tok[1][0]);
      if( (t_chrgCntl.f_pwm >= 0.0f) && (t_chrgCntl.f_pwm < 1.0f))
      {
         s_numBytes = arb_mailboxWrite( t_chrgMlbxHndl,
                                        (int8_t *)&t_chrgCntl,
                                        (int16_t)sizeof( t_chrgManChrgMsg));
      }/*End if( (t_chrgCntl.f_pwm >= 0.0f) && (t_chrgCntl.f_pwm < 1.0f))*/
   }
   else /*Unrecognized message*/
   {
      /*---------------------------------------------------------------------*
       * Let 'arb_console' know the command wasn't found.
       *---------------------------------------------------------------------*/
      b_success = false;
   }

   /*------------------------------------------------------------------------*
    * Return control over the console to the kernel...
    *------------------------------------------------------------------------*/
   return b_success;

}/*End usr_console*/
